home *** CD-ROM | disk | FTP | other *** search
/ Sprite 1984 - 1993 / Sprite 1984 - 1993.iso / src / cmds / gdb-4.5 / dist / bfd / libbfd.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-07-27  |  14.6 KB  |  621 lines

  1. /* libbfd.c -- random BFD support routines, only used internally.
  2.    Copyright (C) 1990-1991 Free Software Foundation, Inc.
  3.    Written by Cygnus Support.
  4.  
  5. This file is part of BFD, the Binary File Descriptor library.
  6.  
  7. This program is free software; you can redistribute it and/or modify
  8. it under the terms of the GNU General Public License as published by
  9. the Free Software Foundation; either version 2 of the License, or
  10. (at your option) any later version.
  11.  
  12. This program is distributed in the hope that it will be useful,
  13. but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  15. GNU General Public License for more details.
  16.  
  17. You should have received a copy of the GNU General Public License
  18. along with this program; if not, write to the Free Software
  19. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.  */
  20.  
  21. /* $Id: libbfd.c,v 1.30 1992/01/24 22:44:23 sac Exp $ */
  22.  
  23. #include "bfd.h"
  24. #include "sysdep.h"
  25. #include "libbfd.h"
  26.  
  27. /*
  28. SECTION
  29.     libbfd
  30.  
  31. DESCRIPTION
  32.     This file contains various routines which are used within BFD.
  33.     They are not intended for export, but are documented here for
  34.     completeness.
  35. */
  36.  
  37. boolean
  38. DEFUN(_bfd_dummy_new_section_hook,(ignore, ignore_newsect),
  39.       bfd *ignore AND
  40.       asection *ignore_newsect)
  41. {
  42.   return true;
  43. }
  44.  
  45. boolean
  46. DEFUN(bfd_false ,(ignore),
  47.       bfd *ignore)
  48. {
  49.   return false;
  50. }
  51.  
  52. boolean
  53. DEFUN(bfd_true,(ignore),
  54.       bfd *ignore)
  55. {
  56.   return true;
  57. }
  58.  
  59. PTR
  60. DEFUN(bfd_nullvoidptr,(ignore),
  61.       bfd *ignore)
  62. {
  63.   return (PTR)NULL;
  64. }
  65.  
  66. int 
  67. DEFUN(bfd_0,(ignore),
  68.       bfd *ignore)
  69. {
  70.   return 0;
  71. }
  72.  
  73. unsigned int 
  74. DEFUN(bfd_0u,(ignore),
  75.       bfd *ignore)
  76. {
  77.    return 0;
  78. }
  79.  
  80. void 
  81. DEFUN(bfd_void,(ignore),
  82.       bfd *ignore)
  83. {
  84. }
  85.  
  86. boolean
  87. DEFUN(_bfd_dummy_core_file_matches_executable_p,(ignore_core_bfd, ignore_exec_bfd),
  88.       bfd *ignore_core_bfd AND
  89.       bfd *ignore_exec_bfd)
  90. {
  91.   bfd_error = invalid_operation;
  92.   return false;
  93. }
  94.  
  95. /* of course you can't initialize a function to be the same as another, grr */
  96.  
  97. char *
  98. DEFUN(_bfd_dummy_core_file_failing_command,(ignore_abfd),
  99.       bfd *ignore_abfd)
  100. {
  101.   return (char *)NULL;
  102. }
  103.  
  104. int
  105. DEFUN(_bfd_dummy_core_file_failing_signal,(ignore_abfd),
  106.      bfd *ignore_abfd)
  107. {
  108.   return 0;
  109. }
  110.  
  111. bfd_target *
  112. DEFUN(_bfd_dummy_target,(ignore_abfd),
  113.      bfd *ignore_abfd)
  114. {
  115.   return 0;
  116. }
  117.  
  118. /** zalloc -- allocate and clear storage */
  119.  
  120.  
  121. #ifndef zalloc
  122. char *
  123. DEFUN(zalloc,(size),
  124.       bfd_size_type size)
  125. {
  126.   char *ptr = (char *) malloc ((int)size);
  127.  
  128.   if ((ptr != NULL) && (size != 0))
  129.    memset(ptr,0, size);
  130.  
  131.   return ptr;
  132. }
  133. #endif
  134.  
  135. /*
  136. INTERNAL_FUNCTION
  137.     bfd_xmalloc
  138.  
  139. SYNOPSIS
  140.     PTR  bfd_xmalloc( bfd_size_type size);
  141.  
  142. DESCRIPTION
  143.     Like malloc, but exit if no more memory.
  144.  
  145. */
  146.  
  147. /** There is major inconsistency in how running out of memory is handled.
  148.   Some routines return a NULL, and set bfd_error to no_memory.
  149.   However, obstack routines can't do this ... */
  150.  
  151.  
  152. DEFUN(PTR bfd_xmalloc,(size),
  153.       bfd_size_type size)
  154. {
  155.   static char no_memory_message[] = "Virtual memory exhausted!\n";
  156.   PTR ptr;
  157.   if (size == 0) size = 1;
  158.   ptr = (PTR)malloc(size);
  159.   if (ptr == NULL)
  160.   if (!ptr)
  161.     {
  162.       write (2, no_memory_message, sizeof(no_memory_message)-1);
  163.       exit (-1);
  164.     }
  165.   return ptr;
  166. }
  167.  
  168. /* Some IO code */
  169.  
  170. /* Note that archive entries don't have streams; they share their parent's.
  171.    This allows someone to play with the iostream behind BFD's back.
  172.  
  173.    Also, note that the origin pointer points to the beginning of a file's
  174.    contents (0 for non-archive elements).  For archive entries this is the
  175.    first octet in the file, NOT the beginning of the archive header. */
  176.  
  177. static 
  178. int DEFUN(real_read,(where, a,b, file),
  179.           PTR where AND
  180.           int a AND
  181.           int b AND
  182.           FILE *file)
  183. {
  184.   /* The original code called fread(where, a, b, file), causing */
  185.   /* millions of calls to bcopy to get individual chars, instead of */
  186.   /* one call to get millions of chars. */
  187.   if (read(fileno(file), where, b*a)) {
  188.     return b;
  189.   }
  190.   return 0;
  191. }
  192. bfd_size_type
  193. DEFUN(bfd_read,(ptr, size, nitems, abfd),
  194.       PTR ptr AND
  195.       bfd_size_type size AND
  196.       bfd_size_type nitems AND
  197.       bfd *abfd)
  198. {
  199.   return (bfd_size_type)real_read (ptr, 1, (int)(size*nitems), bfd_cache_lookup(abfd));
  200. }
  201.  
  202. bfd_size_type
  203. DEFUN(bfd_write,(ptr, size, nitems, abfd),
  204.       CONST PTR ptr AND
  205.       bfd_size_type size AND
  206.       bfd_size_type nitems AND
  207.       bfd *abfd)
  208. {
  209.   return fwrite (ptr, 1, (int)(size*nitems), bfd_cache_lookup(abfd));
  210. }
  211.  
  212. /*
  213. INTERNAL_FUNCTION
  214.     bfd_write_bigendian_4byte_int
  215.  
  216. SYNOPSIS
  217.     void bfd_write_bigendian_4byte_int(bfd *abfd,  int i);
  218.  
  219. DESCRIPTION
  220.     Writes a 4 byte integer to the outputing bfd, in big endian
  221.     mode regardless of what else is going on.  This is usefull in
  222.     archives.
  223.  
  224. */
  225. void
  226. DEFUN(bfd_write_bigendian_4byte_int,(abfd, i),
  227.       bfd *abfd AND
  228.       int i)
  229. {
  230.   bfd_byte buffer[4];
  231.   _do_putb32(i, buffer);
  232.   bfd_write((PTR)buffer, 4, 1, abfd);
  233. }
  234.  
  235. int
  236. DEFUN(bfd_seek,(abfd, position, direction),
  237.       bfd * CONST abfd AND
  238.       CONST file_ptr position AND
  239.       CONST int direction)
  240. {
  241.         /* For the time being, a BFD may not seek to it's end.  The
  242.            problem is that we don't easily have a way to recognize
  243.            the end of an element in an archive. */
  244.  
  245.         BFD_ASSERT(direction == SEEK_SET
  246.                    || direction == SEEK_CUR);
  247.         
  248.         if (direction == SEEK_SET && abfd->my_archive != NULL) 
  249.             {
  250.                     /* This is a set within an archive, so we need to
  251.                        add the base of the object within the archive */
  252.                     return(fseek(bfd_cache_lookup(abfd),
  253.                                  position + abfd->origin,
  254.                                  direction));
  255.             }
  256.         else 
  257.             {
  258.                     return(fseek(bfd_cache_lookup(abfd),  position, direction));
  259.             }   
  260. }
  261.  
  262. long
  263. DEFUN(bfd_tell,(abfd),
  264.       bfd *abfd)
  265. {
  266.         file_ptr ptr;
  267.  
  268.         ptr = ftell (bfd_cache_lookup(abfd));
  269.  
  270.         if (abfd->my_archive)
  271.             ptr -= abfd->origin;
  272.         return ptr;
  273. }
  274.  
  275. /** Make a string table */
  276.  
  277. /*>bfd.h<
  278.  Add string to table pointed to by table, at location starting with free_ptr.
  279.    resizes the table if necessary (if it's NULL, creates it, ignoring
  280.    table_length).  Updates free_ptr, table, table_length */
  281.  
  282. boolean
  283. DEFUN(bfd_add_to_string_table,(table, new_string, table_length, free_ptr),
  284.       char **table AND
  285.       char *new_string AND
  286.       unsigned int *table_length AND
  287.       char **free_ptr)
  288. {
  289.   size_t string_length = strlen (new_string) + 1; /* include null here */
  290.   char *base = *table;
  291.   size_t space_length = *table_length;
  292.   unsigned int offset = (base ? *free_ptr - base : 0);
  293.  
  294.   if (base == NULL) {
  295.     /* Avoid a useless regrow if we can (but of course we still
  296.        take it next time */
  297.     space_length = (string_length < DEFAULT_STRING_SPACE_SIZE ?
  298.                     DEFAULT_STRING_SPACE_SIZE : string_length+1);
  299.     base = zalloc (space_length);
  300.  
  301.     if (base == NULL) {
  302.       bfd_error = no_memory;
  303.       return false;
  304.     }
  305.   }
  306.  
  307.   if ((size_t)(offset + string_length) >= space_length) {
  308.     /* Make sure we will have enough space */
  309.     while ((size_t)(offset + string_length) >= space_length) 
  310.       space_length += space_length/2; /* grow by 50% */
  311.  
  312.     base = (char *) realloc (base, space_length);
  313.     if (base == NULL) {
  314.       bfd_error = no_memory;
  315.       return false;
  316.     }
  317.  
  318.   }
  319.  
  320.   memcpy (base + offset, new_string, string_length);
  321.   *table = base;
  322.   *table_length = space_length;
  323.   *free_ptr = base + offset + string_length;
  324.   
  325.   return true;
  326. }
  327.  
  328. /** The do-it-yourself (byte) sex-change kit */
  329.  
  330. /* The middle letter e.g. get<b>short indicates Big or Little endian
  331.    target machine.  It doesn't matter what the byte order of the host
  332.    machine is; these routines work for either.  */
  333.  
  334. /* FIXME: Should these take a count argument?
  335.    Answer (gnu@cygnus.com):  No, but perhaps they should be inline
  336.                              functions in swap.h #ifdef __GNUC__. 
  337.                              Gprof them later and find out.  */
  338.  
  339. /*
  340. FUNCTION
  341.     bfd_put_size
  342. FUNCTION
  343.     bfd_get_size
  344.  
  345. DESCRIPTION
  346.     These macros as used for reading and writing raw data in
  347.     sections; each access (except for bytes) is vectored through
  348.     the target format of the BFD and mangled accordingly. The
  349.     mangling performs any necessary endian translations and
  350.     removes alignment restrictions. 
  351.  
  352. .#define bfd_put_8(abfd, val, ptr) \
  353. .                (*((char *)ptr) = (char)val)
  354. .#define bfd_get_8(abfd, ptr) \
  355. .                (*((char *)ptr))
  356. .#define bfd_put_16(abfd, val, ptr) \
  357. .                BFD_SEND(abfd, bfd_putx16, (val,ptr))
  358. .#define bfd_get_16(abfd, ptr) \
  359. .                BFD_SEND(abfd, bfd_getx16, (ptr))
  360. .#define bfd_put_32(abfd, val, ptr) \
  361. .                BFD_SEND(abfd, bfd_putx32, (val,ptr))
  362. .#define bfd_get_32(abfd, ptr) \
  363. .                BFD_SEND(abfd, bfd_getx32, (ptr))
  364. .#define bfd_put_64(abfd, val, ptr) \
  365. .                BFD_SEND(abfd, bfd_putx64, (val, ptr))
  366. .#define bfd_get_64(abfd, ptr) \
  367. .                BFD_SEND(abfd, bfd_getx64, (ptr))
  368.  
  369. */ 
  370.  
  371. /*
  372. FUNCTION
  373.     bfd_h_put_size
  374. FUNCTION
  375.     bfd_h_get_size
  376.  
  377. DESCRIPTION
  378.     These macros have the same function as their <<bfd_get_x>>
  379.     bretherin, except that they are used for removing information
  380.     for the header records of object files. Believe it or not,
  381.     some object files keep their header records in big endian
  382.     order, and their data in little endan order.
  383.  
  384. .#define bfd_h_put_8(abfd, val, ptr) \
  385. .                (*((char *)ptr) = (char)val)
  386. .#define bfd_h_get_8(abfd, ptr) \
  387. .                (*((char *)ptr))
  388. .#define bfd_h_put_16(abfd, val, ptr) \
  389. .                BFD_SEND(abfd, bfd_h_putx16,(val,ptr))
  390. .#define bfd_h_get_16(abfd, ptr) \
  391. .                BFD_SEND(abfd, bfd_h_getx16,(ptr))
  392. .#define bfd_h_put_32(abfd, val, ptr) \
  393. .                BFD_SEND(abfd, bfd_h_putx32,(val,ptr))
  394. .#define bfd_h_get_32(abfd, ptr) \
  395. .                BFD_SEND(abfd, bfd_h_getx32,(ptr))
  396. .#define bfd_h_put_64(abfd, val, ptr) \
  397. .                BFD_SEND(abfd, bfd_h_putx64,(val, ptr))
  398. .#define bfd_h_get_64(abfd, ptr) \
  399. .                BFD_SEND(abfd, bfd_h_getx64,(ptr))
  400.  
  401. */ 
  402.  
  403. bfd_vma
  404. DEFUN(_do_getb16,(addr),
  405.       register bfd_byte *addr)
  406. {
  407.         return (addr[0] << 8) | addr[1];
  408. }
  409.  
  410. bfd_vma
  411. DEFUN(_do_getl16,(addr),
  412.       register bfd_byte *addr)
  413. {
  414.         return (addr[1] << 8) | addr[0];
  415. }
  416.  
  417. void
  418. DEFUN(_do_putb16,(data, addr),
  419.       bfd_vma data AND
  420.       register bfd_byte *addr)
  421. {
  422.         addr[0] = (bfd_byte)(data >> 8);
  423.         addr[1] = (bfd_byte )data;
  424. }
  425.  
  426. void
  427. DEFUN(_do_putl16,(data, addr),
  428.       bfd_vma data AND              
  429.       register bfd_byte *addr)
  430. {
  431.         addr[0] = (bfd_byte )data;
  432.         addr[1] = (bfd_byte)(data >> 8);
  433. }
  434.  
  435. bfd_vma
  436. DEFUN(_do_getb32,(addr),
  437.       register bfd_byte *addr)
  438. {
  439.         return ((((addr[0] << 8) | addr[1]) << 8) | addr[2]) << 8 | addr[3];
  440. }
  441.  
  442. bfd_vma
  443. _do_getl32 (addr)
  444.         register bfd_byte *addr;
  445. {
  446.         return ((((addr[3] << 8) | addr[2]) << 8) | addr[1]) << 8 | addr[0];
  447. }
  448.  
  449. bfd_vma
  450. DEFUN(_do_getb64,(addr),
  451.       register bfd_byte *addr)
  452. {
  453. #ifdef HOST_64_BIT
  454.   bfd_64_type low, high;
  455.  
  456.   high= ((((((((addr[0]) << 8) |
  457.               addr[1]) << 8) |
  458.             addr[2]) << 8) |
  459.           addr[3]) );
  460.  
  461.   low = ((((((((addr[4]) << 8) |
  462.               addr[5]) << 8) |
  463.             addr[6]) << 8) |
  464.           addr[7]));
  465.  
  466.   return high << 32 | low;
  467. #else
  468.   BFD_FAIL();
  469.   return 0;
  470. #endif
  471.  
  472. }
  473.  
  474. bfd_vma
  475. DEFUN(_do_getl64,(addr),
  476.       register bfd_byte *addr)
  477. {
  478.  
  479. #ifdef HOST_64_BIT
  480.   bfd_64_type low, high;
  481.   high= (((((((addr[7] << 8) |
  482.               addr[6]) << 8) |
  483.             addr[5]) << 8) |
  484.           addr[4]));
  485.  
  486.   low = (((((((addr[3] << 8) |
  487.               addr[2]) << 8) |
  488.             addr[1]) << 8) |
  489.           addr[0]) );
  490.  
  491.   return high << 32 | low;
  492. #else
  493.   BFD_FAIL();
  494.   return 0;
  495. #endif
  496.  
  497. }
  498.  
  499. void
  500. DEFUN(_do_putb32,(data, addr),
  501.       bfd_vma data AND
  502.       register bfd_byte *addr)
  503. {
  504.         addr[0] = (bfd_byte)(data >> 24);
  505.         addr[1] = (bfd_byte)(data >> 16);
  506.         addr[2] = (bfd_byte)(data >>  8);
  507.         addr[3] = (bfd_byte)data;
  508. }
  509.  
  510. void
  511. DEFUN(_do_putl32,(data, addr),
  512.       bfd_vma data AND
  513.       register bfd_byte *addr)
  514. {
  515.         addr[0] = (bfd_byte)data;
  516.         addr[1] = (bfd_byte)(data >>  8);
  517.         addr[2] = (bfd_byte)(data >> 16);
  518.         addr[3] = (bfd_byte)(data >> 24);
  519. }
  520. void
  521. DEFUN(_do_putb64,(data, addr),
  522.         bfd_vma data AND
  523.         register bfd_byte *addr)
  524. {
  525. #ifdef HOST_64_BIT
  526.   addr[0] = (bfd_byte)(data >> (7*8));
  527.   addr[1] = (bfd_byte)(data >> (6*8));
  528.   addr[2] = (bfd_byte)(data >> (5*8));
  529.   addr[3] = (bfd_byte)(data >> (4*8));
  530.   addr[4] = (bfd_byte)(data >> (3*8));
  531.   addr[5] = (bfd_byte)(data >> (2*8));
  532.   addr[6] = (bfd_byte)(data >> (1*8));
  533.   addr[7] = (bfd_byte)(data >> (0*8));
  534. #else
  535.   BFD_FAIL();
  536. #endif
  537.  
  538. }
  539.  
  540. void
  541. DEFUN(_do_putl64,(data, addr),
  542.       bfd_vma data AND
  543.       register bfd_byte *addr)
  544. {
  545. #ifdef HOST_64_BIT
  546.   addr[7] = (bfd_byte)(data >> (7*8));
  547.   addr[6] = (bfd_byte)(data >> (6*8));
  548.   addr[5] = (bfd_byte)(data >> (5*8));
  549.   addr[4] = (bfd_byte)(data >> (4*8));
  550.   addr[3] = (bfd_byte)(data >> (3*8));
  551.   addr[2] = (bfd_byte)(data >> (2*8));
  552.   addr[1] = (bfd_byte)(data >> (1*8));
  553.   addr[0] = (bfd_byte)(data >> (0*8));
  554. #else
  555.   BFD_FAIL();
  556. #endif
  557.  
  558. }
  559.  
  560.  
  561. /* Default implementation */
  562.  
  563. boolean
  564. DEFUN(bfd_generic_get_section_contents, (abfd, section, location, offset, count),
  565.       bfd *abfd AND
  566.       sec_ptr section AND
  567.       PTR location AND
  568.       file_ptr offset AND
  569.       bfd_size_type count)
  570. {
  571.     if (count == 0)
  572.         return true;
  573.     if ((bfd_size_type)(offset+count) > section->_raw_size
  574.         || bfd_seek(abfd,(file_ptr)( section->filepos + offset), SEEK_SET) == -1
  575.         || bfd_read(location, (bfd_size_type)1, count, abfd) != count)
  576.         return (false); /* on error */
  577.     return (true);
  578. }
  579.  
  580. /* This generic function can only be used in implementations where creating
  581.    NEW sections is disallowed.  It is useful in patching existing sections
  582.    in read-write files, though.  See other set_section_contents functions
  583.    to see why it doesn't work for new sections.  */
  584. boolean
  585. DEFUN(bfd_generic_set_section_contents, (abfd, section, location, offset, count),
  586.       bfd *abfd AND
  587.       sec_ptr section AND
  588.       PTR location AND
  589.       file_ptr offset AND
  590.       bfd_size_type count)
  591. {
  592.     if (count == 0)
  593.         return true;
  594.     if ((bfd_size_type)(offset+count) > bfd_get_section_size_after_reloc(section)
  595.         || bfd_seek(abfd, (file_ptr)(section->filepos + offset), SEEK_SET) == -1
  596.         || bfd_write(location, (bfd_size_type)1, count, abfd) != count)
  597.         return (false); /* on error */
  598.     return (true);
  599. }
  600.  
  601. /*
  602. INTERNAL_FUNCTION
  603.     bfd_log2
  604.  
  605. DESCRIPTION
  606.     Return the log base 2 of the value supplied, rounded up. eg an
  607.     arg of 1025 would return 11.
  608.  
  609. SYNOPSIS
  610.     bfd_vma bfd_log2(bfd_vma x);
  611. */
  612.  
  613. bfd_vma bfd_log2(x)
  614. bfd_vma x;
  615. {
  616.   bfd_vma  result = 0;
  617.   while ( (bfd_vma)(1<< result) < x)
  618.     result++;
  619.   return result;
  620. }
  621.